"use strict";

const jwt = require("jsonwebtoken");

module.exports = () => {
  return async function auth(ctx, next) {
    // 获取请求头信息

    const { authorization } = ctx.request.headers;

    // 判断token是否存在 存在表示已经登录
    if (authorization) {
      jwt.verify(authorization, "1908A");

      await next();
    } else {
      ctx.status = 401;

      ctx.body = {
        code: 0,
        msg: "您还未登录，请先登录",
      };
    }
  };
};
